package cn.tedu.plus.sort;

import java.util.Arrays;
import java.util.Random;

public class ShellSort {
    public static void main(String[] args) {
        Random random = new Random();
        int[] array = new int[1000];
        for (int i = 0; i < 1000; i++) {
            array[i]=random.nextInt(100);
        }
        System.out.println(Arrays.toString(array));

        Long countNum = 0L;
        Long swapNum =0L;
        Long startTime = System.currentTimeMillis();

        for (int gap = array.length/2; gap > 0; gap /= 2) {
            for (int i = gap; i < array.length; i++) {
                for (int j = i - gap; j >= 0 ; j -= gap) {
                    countNum++;
                    if(array[j] < array[j+gap]){
                        int temp = array[j];
                        array[j] = array[j+gap];
                        array[j+gap] = temp;
                        swapNum++;
                    }else {
                        break;
                    }
                }
            }
        }
        Long endTime = System.currentTimeMillis();

        System.out.println("对比次数："+countNum);
        System.out.println("换位次数："+swapNum);
        System.out.println("耗时："+(endTime-startTime)+"毫秒");

        System.out.println(Arrays.toString(array));
    }
}
